package com.eip.zd.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eip.zd.entity.Project;
import com.eip.zd.exception.CustomException;
import com.eip.zd.mapper.ProjectMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProjectService {
    @Resource
    private ProjectMapper projectMapper;

    public List<Project> getProjectList1(String keyword) {
        QueryWrapper<Project> wrapper = new QueryWrapper<>();
        wrapper.like(keyword != null, "name", keyword);
        return projectMapper.selectList(wrapper);
    }

    public List<Project> getProjectList2(String keyword, String custId) {
        return projectMapper.getProjectList2(keyword, custId);
    }

    public void add(Project project) {
        QueryWrapper<Project> wrapper = new QueryWrapper<>();
        wrapper.eq("project_name", project.getProjectName());
        wrapper.eq("deleted", 0);
        Project one = projectMapper.selectOne(wrapper);
        if (one != null) {
            throw new CustomException(500, "该客户已存在");
        }
        projectMapper.insert(project);
    }

    public void update(Project project) {
        projectMapper.updateById(project);
    }

    public void remove(Long id) {
        Project project = projectMapper.selectById(id);
        project.setIfUse(2);
        projectMapper.updateById(project);
        projectMapper.deleteById(id);
    }

    public void ifUse(Long id) {
        Project project = projectMapper.selectById(id);
        if (project.getIfUse() == 1) {
            project.setIfUse(2);
        } else {
            project.setIfUse(1);
        }
        projectMapper.updateById(project);
    }
}
